package com.shujia.MR.combiner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class CombinerDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        Configuration conf = new Configuration();
        // 创建一个Job
        Job job = Job.getInstance(conf);

        job.setJarByClass(CombinerDriver.class);
        job.setJobName("Combiner");

        // 设置Mapper及Reducer类
        job.setMapperClass(CombinerMapper.class);
        job.setReducerClass(CombinerReducer.class);

        // 设置Map方法输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);


        // 设置最终输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        /**
         * 最终输出结果：
         *      Map input records=1098250
         * 		Map output records=2280970
         * 		Map output bytes=25597490
         * 		Map output materialized bytes=232
         * 		Input split bytes=468
         * 		Combine input records=2280970  #对应Map方法输出的数量
         * 		Combine output records=16
         * 		Reduce input groups=4
         * 		Reduce shuffle bytes=232
         * 		Reduce input records=16  #对应Combine输出的结果
         * 		Reduce output records=4
         *
         */
        // 设置预聚合类，其逻辑和Reducer中的逻辑一致
        job.setCombinerClass(CombinerOperator.class);

        // 设置数据源以及输出结果,注意选择 org.apache.hadoop.mapreduce.lib.input.FileInputFormat
        FileInputFormat.addInputPath(job, new Path("D:\\workspace19\\HadoopCode\\data\\combine"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\workspace19\\HadoopCode\\output\\combiner"));

        // 提交
        job.waitForCompletion(true);
    }
}
